package com.beiming.libra.service.impl;

import com.beiming.libra.common.AppException;
import com.beiming.libra.common.enums.ArbitrationCaseStatusEnum;
import com.beiming.libra.common.enums.ErrorCode;
import com.beiming.libra.common.utils.BeanConvertUtils;
import com.beiming.libra.common.utils.CommonsUtils;
import com.beiming.libra.dao.*;
import com.beiming.libra.domain.*;
import com.beiming.libra.domain.dto.requestdto.*;
import com.beiming.libra.service.ArbitrationCaseUserService;
import com.beiming.libra.service.base.BaseServiceImpl;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 案件用户信息更新,删除,增加
 *
 * @author zhangqi
 */
@Service
@Slf4j
public class ArbitrationCaseUserServiceImpl extends BaseServiceImpl<ArbitrationCaseUser> implements
    ArbitrationCaseUserService<ArbitrationCaseUser> {

  @Resource
  private ArbitrationCaseUserMapper arbitrationCaseUserMapper;
  @Resource
  private CaseUserDetailMapper caseUserDetailMapper;
  @Resource
  private UserDetailMapper userDetailMapper;
  @Resource
  private UserMapper userMapper;
  @Resource
  private CaseUserRelationMapper caseUserRelationMapper;

  @Override
  @Transactional
  public int deleteArbitrationCaseUser(String id) {
    Example arbitrationCaseUserExample = new Example(ArbitrationCaseUser.class, false);
    arbitrationCaseUserExample.createCriteria().andEqualTo("id", id);
    ArbitrationCaseUser acu = arbitrationCaseUserMapper
        .selectOneByExample(arbitrationCaseUserExample);
    arbitrationCaseUserMapper.deleteByExample(arbitrationCaseUserExample);

    Example caseUserDetailExample = new Example(CaseUserDetail.class, false);
    arbitrationCaseUserExample.createCriteria().andEqualTo("userId", acu.getUserId())
        .andEqualTo("caseId", acu.getCaseId());
    return caseUserDetailMapper.deleteByExample(caseUserDetailExample);
  }

  @Override
  public int addArbitrationCaseUser(List<CaseUserDetailFormRequestDTO> caseUserDetailFormRequestDTOs) {
    for (CaseUserDetailFormRequestDTO caseUserDetailRequestDTO : caseUserDetailFormRequestDTOs) {
      CaseUserDetail caseUserDetail = new CaseUserDetail();
      ArbitrationCaseUser arbitrationCaseUser = new ArbitrationCaseUser();
        //新增用户信息待完成
      User user = new User();
      if (StringUtils.isEmpty(caseUserDetail.getUserId())) {
        //user表中新增user,再新增userDtail
        user.setId(CommonsUtils.get32BitUUID());
        user.setMobilePhone(caseUserDetailRequestDTO.getMobilePhone());
        user.setUserType(caseUserDetailRequestDTO.getUserType());
        userMapper.insert(user);

        UserDetail userDetail = BeanConvertUtils.copyBean(caseUserDetailRequestDTO, UserDetail.class);
        userDetail.setUserId(user.getId());
        userDetail.setId(CommonsUtils.get32BitUUID());
        userDetailMapper.insert(userDetail);
      }


      //完成个人中心人员信息新增后再新增案件人员详细信息
      caseUserDetail = BeanConvertUtils.copyBean(caseUserDetailRequestDTO, CaseUserDetail.class);
      caseUserDetail.setUserId(user.getId());
      caseUserDetail.setId(CommonsUtils.get32BitUUID());
      //保存案件的用户详情信息
      caseUserDetailMapper.insert(caseUserDetail);

      //保存用户关联信息
      arbitrationCaseUser = new ArbitrationCaseUser();
      arbitrationCaseUser.setUserId(caseUserDetail.getUserId());
      arbitrationCaseUser.setCaseId(caseUserDetail.getCaseId());
      arbitrationCaseUser.setId(CommonsUtils.get32BitUUID());
      arbitrationCaseUser.setUserType(caseUserDetailRequestDTO.getUserBusinessTypeId());
      arbitrationCaseUser.setAddUserStatus(ArbitrationCaseStatusEnum.MODIFY_CASE_USER.getCode());

      arbitrationCaseUserMapper.insert(arbitrationCaseUser);
    }

    return 1;
  }

  /**
   * 通过个人中心增加案件当事人
   *
   * @param caseUserDetailListRequestDTO
   */
  @Override public int addArbitrationCaseUsers(CaseUserDetailListRequestDTO caseUserDetailListRequestDTO) {
    for (UserDetailSimpleRequestDto userDetailSimpleRequestDto : caseUserDetailListRequestDTO.getUserDetailSimpleRequestDtos()) {
      //判断是否已存在在案件中
      ArbitrationCaseUser param = new ArbitrationCaseUser();
      param.setCaseId(caseUserDetailListRequestDTO.getCaseId());
      param.setUserId(userDetailSimpleRequestDto.getUserId());
      List<ArbitrationCaseUser> arbitrationCaseUsers = arbitrationCaseUserMapper.select(param);
      if (!arbitrationCaseUsers.isEmpty()) {
        throw new AppException(ErrorCode.ILLEGAL_PARAMETER, "用户已存在该案件中");
      }

      //从人员列表中添加人员信息
      UserDetail detail = userDetailMapper.selectByPrimaryKey(userDetailSimpleRequestDto.getId());
      CaseUserDetail caseUserDetail = BeanConvertUtils.copyBean(detail, CaseUserDetail.class);
      caseUserDetail.setCaseId(caseUserDetailListRequestDTO.getCaseId());
      caseUserDetail.setId(CommonsUtils.get32BitUUID());
      //保存案件的用户详情信息
      caseUserDetailMapper.insert(caseUserDetail);

      //保存用户关联信息
      ArbitrationCaseUser arbitrationCaseUser = new ArbitrationCaseUser();
      arbitrationCaseUser.setUserId(caseUserDetail.getUserId());
      arbitrationCaseUser.setCaseId(caseUserDetail.getCaseId());
      arbitrationCaseUser.setId(CommonsUtils.get32BitUUID());
      arbitrationCaseUser.setUserType(caseUserDetailListRequestDTO.getUserBusinessTypeId());
      arbitrationCaseUser.setAddUserStatus("0");

      arbitrationCaseUserMapper.insert(arbitrationCaseUser);
    }
    return 1;
  }

  /**
   * 通过表单增加案件代理人,即个人中心无此人数据
   *
   * @param proxyCaseUserDetailFormRequestDTOs
   */
  @Override public int addArbitrationProxyCaseUser(
      List<ProxyCaseUserDetailFormRequestDTO> proxyCaseUserDetailFormRequestDTOs) {
    for (ProxyCaseUserDetailFormRequestDTO proxyCaseUserDetailRequestDTO : proxyCaseUserDetailFormRequestDTOs) {
      CaseUserDetail caseUserDetail = new CaseUserDetail();
      ArbitrationCaseUser arbitrationCaseUser = new ArbitrationCaseUser();
      //新增用户信息待完成
      User user = new User();
      if (StringUtils.isEmpty(caseUserDetail.getUserId())) {
        //user表中新增user,再新增userDtail
        user.setId(CommonsUtils.get32BitUUID());
        user.setMobilePhone(proxyCaseUserDetailRequestDTO.getMobilePhone());
        user.setUserType(proxyCaseUserDetailRequestDTO.getUserType());
        userMapper.insert(user);

        UserDetail userDetail = BeanConvertUtils.copyBean(proxyCaseUserDetailRequestDTO, UserDetail.class);
        userDetail.setUserId(user.getId());
        userDetail.setId(CommonsUtils.get32BitUUID());
        userDetailMapper.insert(userDetail);
      }


      //完成个人中心人员信息新增后再新增案件人员详细信息
      caseUserDetail = BeanConvertUtils.copyBean(proxyCaseUserDetailRequestDTO, CaseUserDetail.class);
      caseUserDetail.setUserId(user.getId());
      caseUserDetail.setId(CommonsUtils.get32BitUUID());
      //保存案件的用户详情信息
      caseUserDetailMapper.insert(caseUserDetail);

      //保存用户关联信息
      arbitrationCaseUser = new ArbitrationCaseUser();
      arbitrationCaseUser.setUserId(caseUserDetail.getUserId());
      arbitrationCaseUser.setCaseId(caseUserDetail.getCaseId());
      arbitrationCaseUser.setId(CommonsUtils.get32BitUUID());
      arbitrationCaseUser.setUserType(proxyCaseUserDetailRequestDTO.getUserBusinessTypeId());
      arbitrationCaseUser.setAddUserStatus("0");

      arbitrationCaseUserMapper.insert(arbitrationCaseUser);
      //保存当事人和代理人关系
      CaseUserRelation caseUserRelation = new CaseUserRelation();
      caseUserRelation.setId(CommonsUtils.get32BitUUID());
      caseUserRelation.setCaseId(caseUserDetail.getCaseId());
      caseUserRelation.setProxyId(proxyCaseUserDetailRequestDTO.getProxiedUserId());
      caseUserRelation.setUserId(user.getId());

      caseUserRelationMapper.insert(caseUserRelation);
    }

    return 1;
  }

  /**
   * 通过个人中心增加案件代理人
   *
   * @param proxyCaseUserDetailListRequestDTO
   */
  @Override public int addArbitrationProxyCaseUsers(
      ProxyCaseUserDetailListRequestDTO proxyCaseUserDetailListRequestDTO) {
    for (UserDetailSimpleRequestDto userDetailSimpleRequestDto : proxyCaseUserDetailListRequestDTO.getUserDetailSimpleRequestDtos()) {
      //判断是否已存在在案件中
      ArbitrationCaseUser param = new ArbitrationCaseUser();
      param.setCaseId(proxyCaseUserDetailListRequestDTO.getCaseId());
      param.setUserId(userDetailSimpleRequestDto.getUserId());
      List<ArbitrationCaseUser> arbitrationCaseUsers = arbitrationCaseUserMapper.select(param);
      if (!arbitrationCaseUsers.isEmpty()) {
        throw new AppException(ErrorCode.ILLEGAL_PARAMETER, "用户已存在该案件中");
      }

      //从人员列表中添加人员信息
      UserDetail detail = userDetailMapper.selectByPrimaryKey(userDetailSimpleRequestDto.getId());
      CaseUserDetail caseUserDetail = BeanConvertUtils.copyBean(detail, CaseUserDetail.class);
      caseUserDetail.setCaseId(proxyCaseUserDetailListRequestDTO.getCaseId());
      caseUserDetail.setId(CommonsUtils.get32BitUUID());
      //保存案件的用户详情信息
      caseUserDetailMapper.insert(caseUserDetail);

      //保存用户关联信息
      ArbitrationCaseUser arbitrationCaseUser = new ArbitrationCaseUser();
      arbitrationCaseUser.setUserId(caseUserDetail.getUserId());
      arbitrationCaseUser.setCaseId(caseUserDetail.getCaseId());
      arbitrationCaseUser.setId(CommonsUtils.get32BitUUID());
      arbitrationCaseUser.setUserType(proxyCaseUserDetailListRequestDTO.getUserBusinessTypeId());
      arbitrationCaseUser.setAddUserStatus("0");

      arbitrationCaseUserMapper.insert(arbitrationCaseUser);

      //保存当事人和代理人关系
      CaseUserRelation caseUserRelation = new CaseUserRelation();
      caseUserRelation.setId(CommonsUtils.get32BitUUID());
      caseUserRelation.setCaseId(caseUserDetail.getCaseId());
      caseUserRelation.setProxyId(proxyCaseUserDetailListRequestDTO.getProxiedUserId());
      caseUserRelation.setUserId(detail.getUserId());

      caseUserRelationMapper.insert(caseUserRelation);
    }
    return 1;
  }

  /**
   * 修改案件当事人
   *
   * @param detail
   */
  @Override public int updateArbitrationCaseUser(CaseUserDetailRequestDTO detail) {
    CaseUserDetail caseUserDetail = caseUserDetailMapper.selectByPrimaryKey(detail.getId());
    if (!StringUtils.isEmpty(detail.getUserType())) {
      caseUserDetail.setUserType(detail.getUserType());
    }
    if (!StringUtils.isEmpty(detail.getUserName())) {
      caseUserDetail.setUserName(detail.getUserName());
    }
    if (!StringUtils.isEmpty(detail.getSex())) {
      caseUserDetail.setSex(detail.getSex());
    }
    if (!StringUtils.isEmpty(detail.getCivilFamily())) {
      caseUserDetail.setCivilFamily(detail.getCivilFamily());
    }
    if (!StringUtils.isEmpty(detail.getCode())) {
      caseUserDetail.setCode(detail.getCode());
    }
    if (!StringUtils.isEmpty(detail.getMobilePhone())) {
      caseUserDetail.setMobilePhone(detail.getMobilePhone());
    }
    if (!StringUtils.isEmpty(detail.getFixedTelephone())) {
      caseUserDetail.setFixedTelephone(detail.getFixedTelephone());
    }
    if (!StringUtils.isEmpty(detail.getPermanentAddress())) {
      caseUserDetail.setPermanentAddress(detail.getPermanentAddress());
    }
    if (!StringUtils.isEmpty(detail.getCurrentAddress())) {
      caseUserDetail.setCurrentAddress(detail.getCurrentAddress());
    }
    if (!StringUtils.isEmpty(detail.getMailAddress())) {
      caseUserDetail.setMailAddress(detail.getMailAddress());
    }
    if (!StringUtils.isEmpty(detail.getPdetailAddress())) {
      caseUserDetail.setPdetailAddress(detail.getPdetailAddress());
    }
    if (!StringUtils.isEmpty(detail.getCdetailAddress())) {
      caseUserDetail.setCdetailAddress(detail.getCdetailAddress());
    }
    if (!StringUtils.isEmpty(detail.getMdetailAddress())) {
      caseUserDetail.setMdetailAddress(detail.getMdetailAddress());
    }

    caseUserDetailMapper.updateByPrimaryKey(caseUserDetail);

    return 1;
  }
}
